package baidu.hudson.plugin.result.collector.MessageRouter;

import org.apache.camel.CamelContext;
import org.apache.camel.Component;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.builder.RouteBuilder;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.jms.JmsException;
import org.springframework.jms.UncategorizedJmsException;

import java.util.List;
import java.util.Map;

/**
 * User: yuxinlei
 * Date: 4/12/11
 * Time: 1:47 PM
 */
public class MessageSender implements InitializingBean {
     protected volatile ProducerTemplate template;
    private volatile CamelContext context;
    protected List<RouteBuilder> routes;
    protected Map<String,Component> components;
    private MessageSenderBuilder messageSenderBuilder;

    private MessageSender(CamelContext context){
        this.setContext(context);

    }

    public MessageSender createMessageSender(CamelContext context, List<RouteBuilder> routes, Map<String,Component> components) {
        return getMessageSenderBuilder().setContext(context).setRoutes(routes).setComponents(components).build();
    }

    public static MessageSender createMessageSender(MessageSenderBuilder messageSenderBuilder) {
        return new MessageSender(messageSenderBuilder.getContext());
    }

    public void sendBodyWithProductTemplate(String queueName , Object object)throws UncategorizedJmsException,JmsException {

             getContext().createProducerTemplate().sendBody(queueName,object);

    }

    public void requestBodyWithProductTemplate(String queueName , Object object){

        getContext().createProducerTemplate().requestBody(queueName,object);
    }


    public void afterPropertiesSet() throws Exception {

    }

    public MessageSenderBuilder getMessageSenderBuilder() {
        return messageSenderBuilder;
    }

    public void setMessageSenderBuilder(MessageSenderBuilder messageSenderBuilder) {
        this.messageSenderBuilder = messageSenderBuilder;
    }

    public CamelContext getContext() {
        return context;
    }

    public void setContext(CamelContext context) {
        this.context = context;
    }
}
